package net.csdn.www;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Test22 extends Thread{
	
	private String key ;
	private String value;
	private TestDo2 testDo;

	public Test22() { }
	
	public Test22(String key1 ,String key2 , String value) {
		this.key = key1 + key2;
		this.value = value;
		testDo = TestDo2.getInstance();
	}

	/**
	 * 刘柱栋    2013-3-12
	 * @param args
	 */
	public static void main(String[] args) {
		Test22 test_1 = new Test22("1" ,"" ,"1");
		Test22 test_2 = new Test22("2" ,"" ,"2");
		Test22 test_3 = new Test22("3" ,"" ,"3");
		Test22 test_4 = new Test22("1" ,"" ,"4");
		
		test_1.start();
		test_2.start();
		test_4.start();
		test_3.start();
		
	}
	
	public void run(){
		while(true){
			testDo.doSome(key, value);
		}
	}

}

class TestDo2 {
	private static TestDo2 testDo = new TestDo2();
	private TestDo2(){}
	
	public static TestDo2 getInstance(){
		return testDo;
	}
	
//	private List<Object> list = new ArrayList<Object>();
	
	private List<Object> list = new CopyOnWriteArrayList<Object>();
	
	
	
	public void doSome(Object key , String value){
		Object o = key ;
		if(!list.contains(o)){
			list.add(o);
		}else{
			for(Iterator<Object> iter = list.iterator() ; iter.hasNext() ; ){
				Object oo =iter.next();
				if(o.equals(oo)){
					o = oo ;
				}
			}
		}
		
		synchronized (o) {
			try {		
				Thread.sleep(1000);
				System.out.println(key + " : " + value + " : " + System.currentTimeMillis() / 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}
